<?php

include 'connect.php';

// var_dump($_SERVER);
if($_SERVER['REQUEST_METHOD'] == "POST"){
    $username = $_POST['username'];
    $password = $_POST['password'];
    if($_POST['log'] == "注册"){
        $sql = "INSERT INTO account (username,password) VALUES ('{$username}','{$password}')";
        $data = $db->query($sql);
        echo "注册成功，请登录";
    }else{
        $sql = "SELECT * FROM account WHERE username = '{$username}' AND password = '{$password}'";
        $data = $db->query($sql);
        if($data->num_rows > 0){
            echo "登录成功 当前登录的账号是：$username  密码：$password </br> 以下是已注册的用户名和密码";

            $sql = "SELECT * FROM account WHERE 1";
            $data = $db->query($sql);
            $rows = array();
            if($data){
                while ($r = $data->fetch_array(MYSQL_ASSOC)) {
                $rows[] = $r;
                }
}


            ?>
            <div>
        <table border="1">
            <tr>
                <th>ID</th>
                <th>用户名</th>
                <th>密码</th>
            </tr>
            <?php
		foreach ($rows as $row ) {
		?>
            <tr>
            <td><?php echo $row['id'];?></td>
            <td><?php echo $row['username'];?></td>
            <td><?php echo $row['password'];?></td>
            
            </tr> 
            <?php
            }
            ?>
            </table>
        </div>
        
        <?php
        }else{
            echo "登录失败";
        }
    }
   
}?>


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>用户登录系统</title>

<style>
body{
    text-align:center;
    margin-top: 250px;
}
</style>
</head>
<body>
<form method="post" action="">
<p>用户名：<input type='text' name='username'/></p>
<p>密  码：<input type='text' name='password'/></p>
<p><input type="submit" name="log" value='登录'/> <input type="submit" name="log" value='注册'/></p>
</form>
</body>

</body>
</html>